﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Options;

namespace BuildHelper
{
    public partial class MyBaseSqlCeForm : ProgNetComponentsDx.Forms.BaseSqlCeForm, IDataForm
    {
        public MyBaseSqlCeForm()
        {
            InitializeComponent();
            if (!this.IsDesignMode())
            {
                OptionCache.OptionChanged += new OptionChangedEventHandler(OptionChanged);
            }
        }

        public MyBaseSqlCeForm(object value):base(value)
        {
            InitializeComponent();
            if (!this.IsDesignMode())
            {
                OptionCache.OptionChanged += new OptionChangedEventHandler(OptionChanged);
            }
        }
       
        ~MyBaseSqlCeForm()
        {
            OptionCache.OptionChanged -= new OptionChangedEventHandler(OptionChanged);
        }
        protected override bool GetRecord()
        {
            bool res = base.GetRecord();
            return res;
        }
        protected virtual void OptionChanged(object optionId)
        {
            OptionSettings s = optionProvider1.GetSettings(bbiAutoLayout);
            if (s != null && s.OptionId.IsNotNull())
            {
                if (OptionCache.IsTheSameId(s.OptionId, Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll))
                {
                    if (OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll))
                    {
                        bbiAutoLayout.Enabled = false;
                        OptionCache.LockObject(bbiAutoLayout);
                        bbiAutoLayout.Checked = true;
                    }
                    else
                    {
                        bbiAutoLayout.Enabled = true;
                        bbiAutoLayout.Checked = OptionCache.GetOptionBoolValue(s.OptionId);
                        OptionCache.UnLockObject(bbiAutoLayout);
                    }
                }
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.MdiFormClosing)
                e.Cancel = true;
            else
            {
                if (!Program.IsExit)
                {
                    FinishEdit();
                    if (changeDetectorProvider1.HasChanges)
                    {
                        DialogResult dlg = this.ShowDxYNCQuestionDialog("Chcesz zapisać zmiany?");
                        switch (dlg)
                        {
                            case DialogResult.Cancel:
                                e.Cancel = true;
                                break;
                            case DialogResult.No:
                                break;
                            case DialogResult.Yes:
                                if (ValidateAll())
                                {
                                    if (!Save())
                                    {
                                        this.ShowDxErrorDialog("Błąd zapisu!");
                                        e.Cancel = true;
                                    }
                                }
                                else
                                {
                                    this.ShowDxWarningDialog("Błąd validacji.");
                                    e.Cancel = true;
                                }
                                break;
                        }
                    }
                }
            }
            base.OnFormClosing(e);
        }
        protected override bool AutoSaveLayout
        {
            get
            {
                if (this.IsDesignMode())
                    return false;
                return base.AutoSaveLayout || OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll);
            }
        }
        protected override void OnLoad()
        {
            if (!this.IsDesignMode())
            {
               
                OptionSettings s = optionProvider1.GetSettings(bbiAutoLayout);
                if (s != null)
                {
                    if (s.OptionId.IsNull())
                    {
                        bbiAutoLayout.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    }
                    else
                    {
                        if (OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Rozne.AutoSaveLayoutAll))
                        {
                            bbiAutoLayout.Enabled = false;
                            OptionCache.LockObject(bbiAutoLayout);
                            bbiAutoLayout.Checked = true;
                        }
                    }
                }
                layoutProvider1.GetSettings(barManager1).SaveLayout = false;
                SetLayoutMenuItemPos();
                bar3.Visible = StatusBarVisible;
                bar2.Visible = MainMenuBarVisible;
                layoutProvider1.SaveCurrentLayoutAsDefault();
            }
            modeProvider1.CurrentMode = ProgNetComponentsDx.Enums.FormMode.None;
            base.OnLoad();
            bar3.Visible = StatusBarVisible;
            bar2.Visible = MainMenuBarVisible;
        }
        protected virtual void SetLayoutMenuItemPos()
        {
            bar1.SetItemPosition(bsiLayout, bar1.LinksPersistInfo.Count);
        }
        protected virtual bool StatusBarVisible
        {
            get { return false; }
        }
        protected virtual bool MainMenuBarVisible
        {
            get { return false; }
        }
        #region IDataForm Members

        string IDataForm.Text
        {
            get { return Text; }
        }

        bool IDataForm.HasChanges
        {
            get { return changeDetectorProvider1.HasChanges; }
        }

        bool IDataForm.Save()
        {
            return Save();
        }

        bool IDataForm.ValidateAll()
        {
            return ValidateAll();
        }

        void IDataForm.FinishEdit()
        {
            FinishEdit();
        }

        #endregion
    }
}
